using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using UDS.Management.Data;
using UDS.Management.Data.Package;


namespace UDS.Build.Database
{
    [LoadInSeparateAppDomain]
    public class DropUnitTestDB :  MarshalByRefObject, ITask
    {
        private string SqlInstance;
        private string DBName;
        protected TaskLoggingHelper Log;

        public DropUnitTestDB()
        {
            Log = new TaskLoggingHelper(this);
        }

        [Required]
        public string DatabaseName
        {
            get
            {
                return DBName;
            }
            set
            {
                DBName = value;
            }
        }

        [Required]
        public string SqlServerInstance
        {
            get
            {
                return SqlInstance;
            }
            set
            {
                SqlInstance = value;
            }
        }

        #region ITask Members

        private IBuildEngine buildEngine;

        public IBuildEngine BuildEngine
        {
            get
            {
                return buildEngine;
            }
            set
            {
                buildEngine = value;
            }
        }

        private ITaskHost hostObject;

        public ITaskHost HostObject
        {
            get
            {
                return hostObject;
            }
            set
            {
                hostObject = value;
            }
        }

        public bool Execute()
        {
            try
            {
                var CreateTestDatabase = new UDS.Management.Data.Database(SqlInstance, DBName);
                CreateTestDatabase.OnUpdate += new UDS.Management.Data.Database.DatabaseUpdateDelegate(TestDatabase_OnUpdate);

                Log.LogMessage(MessageImportance.High, "Dropping database {0} on server {1}", DBName, SqlInstance);
                CreateTestDatabase.DropDatabase(DBName);
                return true;
            }
            catch (System.Data.SqlClient.SqlException sqlEx)
            {
                this.Log.LogErrorFromException(sqlEx);
                this.Log.LogError("If a database server is not installed on this machine, use the BuildUnittestDatabase environment variable to disable database creation.  "
                    + "Use the UnittestSqlServerInstance environment variable to set test database server name.");
                return false;
            }
            catch (Exception e)
            {
                this.Log.LogErrorFromException(e);
                return false;
            }
        }

        private void TestDatabase_OnUpdate(object sender, UDS.Management.Data.Database.DatabaseUpdateEvent update)
        {
            if (!String.IsNullOrEmpty(update.Name))
            {
                this.Log.LogMessage(MessageImportance.High, update.Name);
            }
        }
        #endregion
    }
}
